using System;
using System.Runtime.InteropServices;

using WorkflowCommon = ScenarioFramework.WorkflowEngine.DataModel.Common;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData;

namespace ScenarioFramework.WAPI
{
	/// <summary>
	/// Descrizione di riepilogo per wrpWAPIActivity.
	/// </summary>
	public class Role
	{
		/// <summary>
		/// Initializes a new instance  of the <see cref="Role"/> class.
		/// </summary>
		public Role()
		{
		}

		/// <summary>
		/// Gets the roles.
		/// </summary>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport("WAPI.Dll", EntryPoint="getRoles")]
		private static extern String GetRoles(ref Response response);

		/// <summary>
		/// Gets the roles by key.
		/// </summary>
		/// <param name="sRoleKey">The s role key.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport("WAPI.Dll", EntryPoint="getRolesBykey")]
		private static extern String GetRolesByKey(String sRoleKey, ref Response response);

		/// <summary>
		/// Creates the role.
		/// </summary>
		/// <param name="sRole">The s role.</param>
		/// <param name="response">The response.</param>
		[ DllImport("WAPI.Dll", EntryPoint="createRole")]
		private static extern void CreateRole(String sRole, ref Response response);

		/// <summary>
		/// Updates the role.
		/// </summary>
		/// <param name="sRole">The s role.</param>
		/// <param name="response">The response.</param>
		[ DllImport("WAPI.Dll", EntryPoint="updateRole")]
		private static extern void UpdateRole(String sRole, ref Response response);

		/// <summary>
		/// Deletes the role.
		/// </summary>
		/// <param name="sRoleKey">The s role key.</param>
		/// <param name="response">The response.</param>
		[ DllImport("WAPI.Dll", EntryPoint="deleteRole")]
		private static extern void DeleteRole(String sRoleKey, ref Response response);

		/// <summary>
		/// Adds the user to role.
		/// </summary>
		/// <param name="sUser">The s user.</param>
		/// <param name="sRoleKey">The s role key.</param>
		/// <param name="response">The response.</param>
		[ DllImport("WAPI.Dll", EntryPoint="addUserToRole")]
		private static extern void AddUserToRole(String sUser, String sRoleKey, ref Response response);

		/// <summary>
		/// Removes the user from role.
		/// </summary>
		/// <param name="User">The user.</param>
		/// <param name="sRoleKey">The s role key.</param>
		/// <param name="response">The response.</param>
		[ DllImport("WAPI.Dll", EntryPoint="removeUserFromRole")]
		private static extern void RemoveUserFromRole(String User, String sRoleKey, ref Response response);

		/// <summary>
		/// Gets the roles by user.
		/// </summary>
		/// <param name="sUserKey">The s user key.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport("WAPI.Dll", EntryPoint="getRolesByUser")]
		private static extern String GetRolesByUser(String sUserKey, ref Response response);

		/// <summary>
		/// Checks the grant.
		/// </summary>
		/// <param name="xmlGrants">The XML grants.</param>
		/// <param name="sUser">The s user.</param>
		/// <param name="sGrantType">Type of the s grant.</param>
		/// <returns></returns>
		private static bool CheckGrant(WorkflowCommon.XMLGrants xmlGrants, string sUser, string sGrantType)
		{
			WorkflowCommon.XMLGrant xmlGrant=null;

			bool bRetYes = true;
			bool bRetNo = false;

			for ( int i=0; i<xmlGrants.Count; i++)
			{
				xmlGrant = (WorkflowCommon.XMLGrant)xmlGrants[i];

				if ( xmlGrant.Type == sGrantType ) 
				{
					if ( xmlGrant.Enabled == WorkflowCommon.XMLGrant.YES_ENABLED ) 
					{
						try
						{
							xmlGrant.XMLUsers.GetUser (sUser);
							return true;
						}
                        catch (ScenarioFramework.WorkflowEngine.DataModel.Exception.UserNotFound)
						{
						}

						bool bNotInRole = true;
						XMLRole xmlRole = null;
						for ( int uiRole=0; uiRole<xmlGrant.XMLRoles.Count; uiRole++)
						{
							xmlRole = (XMLRole) xmlGrant.XMLRoles[uiRole];

							Response wapiResponse = new Response();
							string sRole = Role.GetRolesByKey (xmlRole.Key, ref wapiResponse);

							if ( wapiResponse.iCode == 0 )
							{
								XMLRole role = new XMLRole();
								role.Parse(sRole);

								try
								{
									role.XMLUsers.GetUser(sUser);
									bRetYes=true;
									bNotInRole=false;
								}
                                catch (ScenarioFramework.WorkflowEngine.DataModel.Exception.UserNotFound) 
								{
								}
							}
						}

						if ( bNotInRole )
							return false;
					}
					else
					{
						try
						{
							xmlGrant.XMLUsers.GetUser (sUser);
							return false;
						}
                        catch (ScenarioFramework.WorkflowEngine.DataModel.Exception.UserNotFound)
						{
						}

						XMLRole xmlRole = null;
						for ( int uiRole=0; uiRole<xmlGrant.XMLRoles.Count; uiRole++)
						{
							xmlRole = (XMLRole) xmlGrant.XMLRoles[uiRole];

							Response wapiResponse = new Response();
							string sRole = Role.GetRolesByKey (xmlRole.Key, ref wapiResponse);

							if ( wapiResponse.iCode == 0 )
							{
								XMLRole role = new XMLRole();
								role.Parse(sRole);

								try
								{
									role.XMLUsers.GetUser(sUser);
									bRetNo = true;
								}
                                catch (ScenarioFramework.WorkflowEngine.DataModel.Exception.UserNotFound) 
								{
								}
							}
						}
					}
				}
			}

			return bRetYes && !bRetNo;
		}
	}
}
